package com.chenglu.android.classify.mvp.presenter;




import com.chenglu.android.classify.bean.CategoryBannerBean;
import com.chenglu.android.classify.bean.ClassisyTreeBean;
import com.chenglu.android.classify.mvp.contract.ClassifyTreeContract;
import com.chenglu.android.classify.mvp.model.ClassifyTreeModel;
import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.RetrofitClient;
import com.chenglu.android.common.utils.LogUtils;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.annotations.Nullable;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subjects.Subject;


/**
 * @author: lyj
 * @Created by Copyright 2023/3/2 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class ClassifyTreePresenter extends BasePresenter<ClassifyTreeContract.View> implements ClassifyTreeContract.ClassifyTreePresenter {

     private  ClassifyTreeContract.ClassifyTreeModel model;

    public ClassifyTreePresenter(){
        model = new ClassifyTreeModel();
    }

    @Override
    public void getClassisyTree() {

        if(!isViewAttached()){
            return;
        }

        Observable observable1 = RetrofitClient.getInstance().getApi().getClassisyTree();
        Observable observable2 = RetrofitClient.getInstance().getApi().getBanner();
        Observable.merge(observable1, observable2)    // 这里将 要请求的接口信息 放进来  可以放多个
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subject() {
                    @Override
                    public boolean hasObservers() {
                        return false;
                    }

                    @Override
                    public boolean hasThrowable() {
                        return false;
                    }

                    @Override
                    public boolean hasComplete() {
                        return false;
                    }

                    @Override
                    public @Nullable Throwable getThrowable() {
                        return null;
                    }

                    @Override
                    protected void subscribeActual(@NonNull Observer observer) {

                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(Object obj) {

                        if (obj instanceof ClassisyTreeBean) {
                            ClassisyTreeBean classisyTreeBean = (ClassisyTreeBean) obj;
                            mView.getClassisyTree(classisyTreeBean);
                            LogUtils.e("树形分类" + classisyTreeBean.toString());
                        } else if (obj instanceof CategoryBannerBean) {
                            CategoryBannerBean banner = (CategoryBannerBean) obj;
                            mView.getBanner(banner);
                            LogUtils.e("banner" + banner.toString());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        dismissLoading();
                        LogUtils.v("树形分类" + e.getMessage());
//                        mView.onError(e.getMessage());

                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();

                    }
                });
    }



}
